<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
$return['basic']=$this->plugin_setting('basic');
if($_RQ['op']=='get'){
	if($_SESSION['uid']){
		$member=$this->model('member')->get_member();
	}else{
		$return['ischecklogin']=$this->model('member')->get_member();
	}
	$this->model('member')->checkmyinfo();
	$return['basic']['jifen_rate']=$return['basic']['jifen_rate']?:1;
	$redfeeset=$return['basic']['redfeeset'];
	if(!$redfeeset){
		$redfeeset[]=array(
			'fee'=>5,
			'addnum'=>15,
			'binum'=>$return['basic']['jifen_rate']*5,
		);
		$redfeeset[]=array(
			'fee'=>10,
			'addnum'=>35,
			'binum'=>$return['basic']['jifen_rate']*10,
		);
		$redfeeset[]=array(
			'fee'=>50,
			'addnum'=>200,
			'binum'=>$return['basic']['jifen_rate']*50,
		);
	}else{
		foreach($redfeeset as &$rv){
			$rv['binum']=intval($return['basic']['jifen_rate']*$rv['fee']*100)/100;
		}
	}
	$return['item']=pdo_get('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('id','credit1','credit2','credit3','mobile','groupid','avatar','nickname'));
	$return['item']['money']=$redfeeset[0]['fee'];
	$return['item']['key']=0;
	$return['redfeeset']=$redfeeset;
	exi($return);
}
if($_RQ['op']=='recharge'){
	$post=get_postdata();
	if(intval($post['money'])<=0){
		exi('请先设置充值金额！并且为整数！','error');
	}
	$redfeeset=$return['basic']['redfeeset'];
	if(!$redfeeset){
		$redfeeset[]=array(
			'fee'=>5,
			'addnum'=>15,
			'binum'=>$return['basic']['jifen_rate']*5,
		);
		$redfeeset[]=array(
			'fee'=>10,
			'addnum'=>35,
			'binum'=>$return['basic']['jifen_rate']*10,
		);
		$redfeeset[]=array(
			'fee'=>50,
			'addnum'=>200,
			'binum'=>$return['basic']['jifen_rate']*50,
		);
	}else{
		foreach($redfeeset as &$rv){
			$rv['binum']=intval($return['basic']['jifen_rate']*$rv['fee']*100)/100;;
		}
	}
	$key=$post['key'];
	$select=$redfeeset[$key];
	$fee=sprintf("%.2f", intval($select['fee']));
	$data=array(
		'uniacid'=>$_SESSION['uniacid'],
		'mid'=>$_SESSION['uid'],
		'fee'=>$fee,
		'credit'=>$select['binum']+$select['addnum'],
		'type'=>3,
		'wxapp_id'=>$_SESSION['appgid'],
		'createtime'=>TIMESTAMP
	);
	pdo_insert('core_recharge',$data);
	$id=pdo_insertid();
	$params=array(
		'action'=>'recharge',
		'id'=>$id,
		'fee'=>$fee,
		'title'=>'金币充值',
		'plugin'=>'core',
		//'pay_type'=>'wechat',
		'no_type'=>array('hdfk'),
		'back_url'=>'jy_ailiao/member.jinbibuy/pay_result',
		'success_url'=>'jy_ailiao/member.index//type=3',
		'error_url'=>'jy_ailiao/member.index//type=3',
	);
	if($_SESSION['container']=='aliapp' && $_SESSION['aliappopenid']){
		$params['pay_type']='alipay';
	}
	$pay_id=cfc('pay')->create_pay_order($params);
	pdo_update('core_recharge',array('pay_id'=>$pay_id),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	exi($pay_id);
}
if($_RQ['op']=='pay_result'){
	$payresult=$_SESSION['pay_result'];
	$recharge=pdo_get('core_recharge',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	if($recharge['fee']!=$payresult['fee']){
		exi('支付失败，支付金额不匹配！','error');
	}
	if($recharge['status']){
		exi('该订单已支付！无需重复支付！','error');
	}
	if($payresult['status']!=1){
		exi('充值失败！','error');
	}
	pdo_update('core_recharge',array('status'=>1,'pay_type'=>$payresult['pay_type']),array('id'=>$recharge['id'],'uniacid'=>$_SESSION['uniacid']));
	cfc('core')->member_credit($_SESSION['uid'],'credit3',$recharge['credit'],1,'jy_ailiao','会员自助充值');
	$recharge['ordersn']=$recharge['id'];
	$recharge['money']=$recharge['fee'];
	$this->model('fuwu')->create($recharge);
	exi('充值成功！');
}